﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing.ext {
    import flash.events.*;
    import org.aswing.*;
    import org.aswing.geom.*;
    import org.aswing.event.*;
    import org.aswing.util.*;

    public class GridList extends JViewport implements ListDataListener {

        public static var SINGLE_SELECTION:int = 0;
        public static var MULTIPLE_SELECTION:int = 1;

        protected var tileHolder:GridCellHolder;
        protected var gridLayout:GridListLayout;
        protected var cellFactory:GridListCellFactory;
        protected var model:ListModel;
        protected var selectionModel:ListSelectionModel;
        protected var cells:ArrayList;
        protected var selectable:Boolean = true;
        protected var autoScroll:Boolean = true;
        protected var tileWidth:int = 40;
        protected var tileHeight:int = 20;
        private var pressedIndex:Number;
        private var pressedCtrl:Boolean;
        private var pressedShift:Boolean;
        private var doSelectionWhenRelease:Boolean;

        public function GridList(_arg1:ListModel=null, _arg2:GridListCellFactory=null, _arg3:int=0, _arg4:int=2, _arg5:Boolean=true){
            super(this.createHolder(_arg3, _arg4));
            if (_arg5){
                setHorizontalAlignment(AsWingConstants.LEFT);
            } else {
                setHorizontalAlignment(AsWingConstants.CENTER);
            };
            setVerticalAlignment(AsWingConstants.TOP);
            this.cells = new ArrayList();
            if (_arg1 == null){
                _arg1 = new VectorListModel();
            };
            if (_arg2 == null){
                _arg2 = new GeneralGridListCellFactory(DefaultGridCell);
            };
            this.cellFactory = _arg2;
            this.setSelectionModel(new DefaultListSelectionModel());
            this.setModel(_arg1);
        }
        public function getCells():ArrayList{
            return (this.cells);
        }
        public function setSelectable(_arg1:Boolean):void{
            this.selectable = _arg1;
        }
        public function isSelectable():Boolean{
            return (this.selectable);
        }
        public function setTileWidth(_arg1:int):void{
            if (this.tileWidth != _arg1){
                this.tileWidth = _arg1;
                this.gridLayout.setTileWidth(_arg1);
                this.tileHolder.revalidate();
            };
        }
        public function setTileHeight(_arg1:int):void{
            if (this.tileHeight != _arg1){
                this.tileHeight = _arg1;
                this.gridLayout.setTileHeight(_arg1);
                this.tileHolder.revalidate();
            };
        }
        public function getTileWidth():int{
            return (this.tileWidth);
        }
        public function getTileHeight():int{
            return (this.tileHeight);
        }
        public function setAutoScroll(_arg1:Boolean):void{
            this.autoScroll = _arg1;
        }
        public function isAutoScroll():Boolean{
            return (this.autoScroll);
        }
        protected function createHolder(_arg1:int, _arg2:int):Container{
            this.tileHolder = new GridCellHolder(this);
            this.gridLayout = new GridListLayout(_arg2, _arg1, 2, 2);
            this.gridLayout.setTileWidth(this.getTileWidth());
            this.gridLayout.setTileHeight(this.getTileHeight());
            this.tileHolder.setLayout(this.gridLayout);
            return (this.tileHolder);
        }
        public function setHolderLayout(_arg1:GridListLayout):void{
            if (_arg1 != this.gridLayout){
                this.gridLayout = _arg1;
                this.gridLayout.setTileWidth(this.getTileWidth());
                this.gridLayout.setTileHeight(this.getTileHeight());
                this.tileHolder.setLayout(this.gridLayout);
            };
        }
        public function setModel(_arg1:ListModel):void{
            if (_arg1 != this.model){
                if (this.model != null){
                    this.model.removeListDataListener(this);
                };
                this.model = _arg1;
                this.model.addListDataListener(this);
                this.rebuildListView();
            };
        }
        public function setListData(_arg1:Array):void{
            var _local2:ListModel = new VectorListModel(_arg1);
            this.setModel(_local2);
        }
        public function getModel():ListModel{
            return (this.model);
        }
        public function setSelectionModel(_arg1:ListSelectionModel):void{
            if (_arg1 != this.selectionModel){
                if (this.selectionModel != null){
                    this.selectionModel.removeListSelectionListener(this.__selectionListener);
                };
                this.selectionModel = _arg1;
                this.selectionModel.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
                this.selectionModel.addListSelectionListener(this.__selectionListener);
            };
        }
        public function getSelectionModel():ListSelectionModel{
            return (this.selectionModel);
        }
        public function setSelectionMode(_arg1:int):void{
            this.getSelectionModel().setSelectionMode(_arg1);
        }
        public function getSelectionMode():int{
            return (this.getSelectionModel().getSelectionMode());
        }
        public function addSelectionListener(_arg1:Function, _arg2:int=0, _arg3:Boolean=false):void{
            addEventListener(SelectionEvent.LIST_SELECTION_CHANGED, _arg1, false, _arg2, _arg3);
        }
        public function clearSelection(_arg1:Boolean=true):void{
            this.selectionModel.clearSelection(_arg1);
        }
        public function setColsRows(_arg1:int, _arg2:int):void{
            this.gridLayout.setColumns(_arg1);
            this.gridLayout.setRows(_arg2);
            this.tileHolder.revalidate();
            revalidate();
        }
        public function setColumns(_arg1:int):void{
            this.gridLayout.setColumns(_arg1);
            this.tileHolder.revalidate();
            revalidate();
        }
        public function setRows(_arg1:int):void{
            this.gridLayout.setRows(_arg1);
            this.tileHolder.revalidate();
            revalidate();
        }
        public function getColumns():int{
            return (this.gridLayout.getColumns());
        }
        public function getRows():int{
            return (this.gridLayout.getRows());
        }
        public function setHGap(_arg1:int):void{
            this.gridLayout.setHgap(_arg1);
            this.tileHolder.revalidate();
            revalidate();
        }
        public function setVGap(_arg1:int):void{
            this.gridLayout.setVgap(_arg1);
            this.tileHolder.revalidate();
            revalidate();
        }
        public function getHGap():int{
            return (this.gridLayout.getHgap());
        }
        public function getVGap():int{
            return (this.gridLayout.getVgap());
        }
        public function getAnchorSelectionIndex():int{
            return (this.getSelectionModel().getAnchorSelectionIndex());
        }
        public function getLeadSelectionIndex():int{
            return (this.getSelectionModel().getLeadSelectionIndex());
        }
        public function setSelectionInterval(_arg1:int, _arg2:int, _arg3:Boolean=true):void{
            this.getSelectionModel().setSelectionInterval(_arg1, _arg2, _arg3);
        }
        public function addSelectionInterval(_arg1:int, _arg2:int, _arg3:Boolean=true):void{
            this.getSelectionModel().addSelectionInterval(_arg1, _arg2, _arg3);
        }
        public function removeSelectionInterval(_arg1:int, _arg2:int, _arg3:Boolean=true):void{
            this.getSelectionModel().removeSelectionInterval(_arg1, _arg2, _arg3);
        }
        public function selectAll(_arg1:Boolean=true):void{
            this.setSelectionInterval(0, (this.getModel().getSize() - 1), _arg1);
        }
        public function setSelectedIndex(_arg1:int, _arg2:Boolean=true):void{
            if (_arg1 >= this.getModel().getSize()){
                return;
            };
            this.getSelectionModel().setSelectionInterval(_arg1, _arg1, _arg2);
        }
        public function getSelectedIndex():int{
            return (this.getSelectionModel().getMinSelectionIndex());
        }
        public function isSelectionEmpty():Boolean{
            return (this.getSelectionModel().isSelectionEmpty());
        }
        public function isSelectedIndex(_arg1:int):Boolean{
            return (this.getSelectionModel().isSelectedIndex(_arg1));
        }
        public function setSelectedValue(_arg1, _arg2:Boolean=true):void{
            var _local3:int = this.model.getSize();
            var _local4:int;
            while (_local4 < _local3) {
                if (this.model.getElementAt(_local4) == _arg1){
                    this.setSelectedIndex(_local4, _arg2);
                    return;
                };
                _local4++;
            };
            this.setSelectedIndex(-1, _arg2);
        }
        public function getSelectedValue(){
            if (this.isSelectionEmpty()){
                return (null);
            };
            return (this.getModel().getElementAt(this.getSelectedIndex()));
        }
        public function getSelectedValues():Array{
            var _local1:Array = new Array();
            var _local2:ListSelectionModel = this.getSelectionModel();
            var _local3:int = _local2.getMinSelectionIndex();
            var _local4:int = _local2.getMaxSelectionIndex();
            if ((((((_local3 < 0)) || ((_local4 < 0)))) || (this.isSelectionEmpty()))){
                return (_local1);
            };
            var _local5:ListModel = this.getModel();
            var _local6:int = _local3;
            while (_local6 <= _local4) {
                if (_local2.isSelectedIndex(_local6)){
                    _local1.push(_local5.getElementAt(_local6));
                };
                _local6++;
            };
            return (_local1);
        }
        public function getSelectedIndices():Array{
            var _local1:Array = new Array();
            var _local2:ListSelectionModel = this.getSelectionModel();
            var _local3:int = _local2.getMinSelectionIndex();
            var _local4:int = _local2.getMaxSelectionIndex();
            if ((((((_local3 < 0)) || ((_local4 < 0)))) || (this.isSelectionEmpty()))){
                return (_local1);
            };
            var _local5:int = _local3;
            while (_local5 <= _local4) {
                if (_local2.isSelectedIndex(_local5)){
                    _local1.push(_local5);
                };
                _local5++;
            };
            return (_local1);
        }
        public function setSelectedIndices(_arg1:Array, _arg2:Boolean=true):void{
            var _local3:ListSelectionModel = this.getSelectionModel();
            _local3.clearSelection();
            var _local4:int = this.getModel().getSize();
            var _local5:int;
            while (_local5 < _arg1.length) {
                if (_arg1[_local5] < _local4){
                    _local3.addSelectionInterval(_arg1[_local5], _arg1[_local5], _arg2);
                };
                _local5++;
            };
        }
        public function setSelectedValues(_arg1:Array, _arg2:Boolean=true):void{
            var _local6:int;
            var _local3:ListSelectionModel = this.getSelectionModel();
            _local3.clearSelection();
            var _local4:int = this.getModel().getSize();
            var _local5:int;
            while (_local5 < _arg1.length) {
                _local6 = 0;
                while (_local6 < _local4) {
                    if (_arg1[_local5] == this.getModel().getElementAt(_local6)){
                        _local3.addSelectionInterval(_local6, _local6, _arg2);
                        break;
                    };
                    _local6++;
                };
                _local5++;
            };
        }
        public function getCellByIndex(_arg1:int):GridListCell{
            return (this.cells.elementAt(_arg1));
        }
        public function scrollToView(_arg1):void{
            var _local2:int = this.model.getSize();
            var _local3:int;
            while (_local3 < _local2) {
                if (this.model.getElementAt(_local3) == _arg1){
                    this.scrollToViewIndex(_local3);
                    return;
                };
                _local3++;
            };
        }
        public function scrollToViewIndex(_arg1:int):void{
            var _local2:Component;
            var _local3:IntPoint;
            var _local4:IntPoint;
            var _local5:IntDimension;
            if ((((_arg1 >= 0)) && ((_arg1 < this.model.getSize())))){
                _local2 = this.tileHolder.getComponent(_arg1);
                _local3 = _local2.getLocation();
                _local4 = getViewPosition();
                _local5 = getExtentSize();
                if ((_local3.x + this.gridLayout.getTileWidth()) > (_local4.x + _local5.width)){
                    _local4.x = ((_local3.x + this.gridLayout.getTileWidth()) - _local5.width);
                };
                if ((_local3.y + this.gridLayout.getTileHeight()) > (_local4.y + _local5.height)){
                    _local4.y = ((_local3.y + this.gridLayout.getTileHeight()) - _local5.height);
                };
                if (_local3.x < _local4.x){
                    _local4.x = _local3.x;
                };
                if (_local3.y < _local4.y){
                    _local4.y = _local3.y;
                };
                setViewPosition(_local4);
            };
        }
        override public function getVerticalUnitIncrement():int{
            return ((this.gridLayout.getTileHeight() + this.gridLayout.getVgap()));
        }
        override public function getVerticalBlockIncrement():int{
            return ((getInsets().getInsideSize(getSize()).height - this.gridLayout.getVgap()));
        }
        override public function getHorizontalUnitIncrement():int{
            return ((this.gridLayout.getTileWidth() + this.gridLayout.getHgap()));
        }
        override public function getHorizontalBlockIncrement():int{
            return ((getInsets().getInsideSize(getSize()).width - this.gridLayout.getHgap()));
        }
        private function rebuildListView():void{
            var _local1:GridListCell;
            this.clearSelection();
            var _local2:int;
            while (_local2 < this.cells.size()) {
                _local1 = this.cells.get(_local2);
                this.removeHandlersFromCell(_local1.getCellComponent());
                _local2++;
            };
            this.tileHolder.removeAll();
            this.cells.clear();
            var _local3:ListModel = this.getModel();
            _local2 = 0;
            while (_local2 < _local3.getSize()) {
                _local1 = this.createNewCell();
                this.cells.append(_local1, _local2);
                _local1.setCellValue(_local3.getElementAt(_local2));
                _local1.setGridListCellStatus(this, false, _local2);
                this.addCellToContainer(_local1, _local2);
                _local2++;
            };
            this.tileHolder.revalidate();
            revalidate();
        }
        private function __selectionListener(_arg1:SelectionEvent):void{
            var _local4:GridListCell;
            var _local2:int = this.cells.size();
            var _local3:int;
            while (_local3 < _local2) {
                _local4 = this.cells.elementAt(_local3);
                _local4.setGridListCellStatus(this, this.getSelectionModel().isSelectedIndex(_local3), _local3);
                _local3++;
            };
            dispatchEvent(new SelectionEvent(SelectionEvent.LIST_SELECTION_CHANGED, _arg1.getFirstIndex(), _arg1.getLastIndex(), _arg1.isProgrammatic()));
            if (((!(this.getSelectionModel().isSelectionEmpty())) && (this.isAutoScroll()))){
                this.scrollToViewIndex(_arg1.getLastIndex());
            };
        }
        override public function getViewSize():IntDimension{
            if (getView() == null){
                return (new IntDimension());
            };
            if (((isTracksWidth()) && (isTracksHeight()))){
                return (getExtentSize());
            };
            return (this.gridLayout.getViewSize(this.tileHolder));
        }
        protected function createNewCell():GridListCell{
            return (this.cellFactory.createNewGridListCell());
        }
        protected function addCellToContainer(_arg1:GridListCell, _arg2:int):void{
            this.tileHolder.insert(_arg2, _arg1.getCellComponent());
            this.addHandlersToCell(_arg1.getCellComponent());
        }
        protected function removeCellFromeContainer(_arg1:GridListCell):void{
            this.tileHolder.remove(_arg1.getCellComponent());
            this.removeHandlersFromCell(_arg1.getCellComponent());
        }
        public function intervalAdded(_arg1:ListDataEvent):void{
            var _local6:GridListCell;
            var _local2:ListModel = this.getModel();
            var _local3:int = Math.min(_arg1.getIndex0(), _arg1.getIndex1());
            var _local4:int = Math.max(_arg1.getIndex0(), _arg1.getIndex1());
            var _local5:int = _local3;
            while (_local5 <= _local4) {
                _local6 = this.createNewCell();
                this.cells.append(_local6, _local5);
                _local6.setCellValue(_local2.getElementAt(_local5));
                _local6.setGridListCellStatus(this, false, _local5);
                this.addCellToContainer(_local6, _local5);
                _local5++;
            };
            this.selectionModel.insertIndexInterval(_local3, ((_local4 - _local3) + 1), true);
            this.tileHolder.revalidate();
            revalidate();
        }
        public function intervalRemoved(_arg1:ListDataEvent):void{
            var _local4:int;
            var _local5:GridListCell;
            var _local2:int = Math.min(_arg1.getIndex0(), _arg1.getIndex1());
            var _local3:int = Math.max(_arg1.getIndex0(), _arg1.getIndex1());
            _local4 = _local2;
            while (_local4 <= _local3) {
                _local5 = GridListCell(this.cells.get(_local4));
                this.removeCellFromeContainer(_local5);
                _local4++;
            };
            this.cells.removeRange(_local2, _local3);
            this.selectionModel.removeIndexInterval(_local2, _local3);
            this.tileHolder.revalidate();
            revalidate();
        }
        public function contentsChanged(_arg1:ListDataEvent):void{
            var _local6:GridListCell;
            var _local2:ListModel = this.getModel();
            var _local3:int = Math.min(_arg1.getIndex0(), _arg1.getIndex1());
            var _local4:int = Math.max(_arg1.getIndex0(), _arg1.getIndex1());
            var _local5:int = _local3;
            while (_local5 <= _local4) {
                _local6 = this.cells.get(_local5);
                _local6.setCellValue(_local2.getElementAt(_local5));
                _local5++;
            };
        }
        protected function addHandlersToCell(_arg1:Component):void{
            _arg1.addEventListener(MouseEvent.CLICK, this.__onItemClick);
            _arg1.addEventListener(MouseEvent.DOUBLE_CLICK, this.__onItemDoubleClick);
            _arg1.addEventListener(MouseEvent.MOUSE_DOWN, this.__onItemMouseDown);
            _arg1.addEventListener(MouseEvent.ROLL_OVER, this.__onItemRollOver);
            _arg1.addEventListener(MouseEvent.ROLL_OUT, this.__onItemRollOut);
            _arg1.addEventListener(ReleaseEvent.RELEASE_OUT_SIDE, this.__onItemReleaseOutSide);
        }
        protected function removeHandlersFromCell(_arg1:Component):void{
            _arg1.removeEventListener(MouseEvent.CLICK, this.__onItemClick);
            _arg1.removeEventListener(MouseEvent.DOUBLE_CLICK, this.__onItemDoubleClick);
            _arg1.removeEventListener(MouseEvent.MOUSE_DOWN, this.__onItemMouseDown);
            _arg1.removeEventListener(MouseEvent.ROLL_OVER, this.__onItemRollOver);
            _arg1.removeEventListener(MouseEvent.ROLL_OUT, this.__onItemRollOut);
            _arg1.removeEventListener(ReleaseEvent.RELEASE_OUT_SIDE, this.__onItemReleaseOutSide);
        }
        private function __onItemMouseDownSelection(_arg1:GridListItemEvent):void{
            var _local2:int = this.cells.indexOf(_arg1.getCell());
            this.pressedIndex = _local2;
            this.pressedCtrl = _arg1.ctrlKey;
            this.pressedShift = _arg1.shiftKey;
            this.doSelectionWhenRelease = false;
            if (this.getSelectionMode() == MULTIPLE_SELECTION){
                if (this.getSelectionModel().isSelectedIndex(_local2)){
                    this.doSelectionWhenRelease = true;
                } else {
                    this.doSelection();
                };
            } else {
                this.getSelectionModel().setSelectionInterval(_local2, _local2, false);
            };
        }
        private function doSelection():void{
            var _local2:int;
            var _local1:int = this.pressedIndex;
            if (this.pressedShift){
                _local2 = this.getSelectionModel().getAnchorSelectionIndex();
                if (_local2 < 0){
                    _local2 = _local1;
                };
                this.getSelectionModel().setSelectionInterval(_local2, _local1, false);
            } else {
                if (this.pressedCtrl){
                    if (!this.isSelectedIndex(_local1)){
                        this.getSelectionModel().addSelectionInterval(_local1, _local1, false);
                    } else {
                        this.getSelectionModel().removeSelectionInterval(_local1, _local1, false);
                    };
                } else {
                    this.getSelectionModel().setSelectionInterval(_local1, _local1, false);
                };
            };
        }
        private function __onItemClickSelection(_arg1:GridListItemEvent):void{
            if (this.doSelectionWhenRelease){
                this.doSelection();
                this.doSelectionWhenRelease = false;
            };
        }
        protected function createItemEventObj(_arg1, _arg2:String, _arg3:MouseEvent):GridListItemEvent{
            var _local4:int = this.tileHolder.getIndex(_arg1);
            var _local5:GridListCell = this.cells.get(_local4);
            var _local6:GridListItemEvent = new GridListItemEvent(_arg2, this.getModel().getElementAt(_local4), _local4, _local5, _arg3);
            return (_local6);
        }
        private function __onItemMouseDown(_arg1:MouseEvent):void{
            var _local2:GridListItemEvent = this.createItemEventObj(_arg1.currentTarget, GridListItemEvent.ITEM_MOUSE_DOWN, _arg1);
            if (this.isSelectable()){
                this.__onItemMouseDownSelection(_local2);
            };
            dispatchEvent(_local2);
        }
        private function __onItemClick(_arg1:MouseEvent):void{
            var _local2:GridListItemEvent = this.createItemEventObj(_arg1.currentTarget, GridListItemEvent.ITEM_CLICK, _arg1);
            if (this.isSelectable()){
                this.__onItemClickSelection(_local2);
            };
            dispatchEvent(_local2);
        }
        private function __onItemReleaseOutSide(_arg1:ReleaseEvent):void{
            dispatchEvent(this.createItemEventObj(_arg1.currentTarget, GridListItemEvent.ITEM_RELEASE_OUT_SIDE, _arg1));
        }
        private function __onItemRollOver(_arg1:MouseEvent):void{
            dispatchEvent(this.createItemEventObj(_arg1.currentTarget, GridListItemEvent.ITEM_ROLL_OVER, _arg1));
        }
        private function __onItemRollOut(_arg1:MouseEvent):void{
            dispatchEvent(this.createItemEventObj(_arg1.currentTarget, GridListItemEvent.ITEM_ROLL_OUT, _arg1));
        }
        private function __onItemDoubleClick(_arg1:MouseEvent):void{
            dispatchEvent(this.createItemEventObj(_arg1.currentTarget, GridListItemEvent.ITEM_DOUBLE_CLICK, _arg1));
        }

    }
}//package org.aswing.ext 
